package com.XG.noteapp

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.XG.noteapp.data.Note
import com.XG.noteapp.roomsql.AppDatabase
import com.bumptech.glide.Glide
import com.google.android.material.button.MaterialButton
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class DetailActivity : AppCompatActivity() {

    private lateinit var noteDatabase: AppDatabase
    private lateinit var note: Note
    private lateinit var titleEditText: EditText
    private lateinit var contentEditText: EditText
    private lateinit var imageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)

        // 初始化视图
        titleEditText = findViewById(R.id.detail_title_edittext)
        contentEditText = findViewById(R.id.detail_content_edittext)
        imageView = findViewById(R.id.detail_image)

        // 初始化数据库
        noteDatabase = AppDatabase.getInstance(this)

        // 从Intent中获取笔记ID
        val noteId = intent.getLongExtra("note_id", -1)
        if (noteId == -1L) {
            Toast.makeText(this, "笔记ID无效", Toast.LENGTH_SHORT).show()
            finish()
            return
        }

        // 根据笔记ID加载笔记数据
        lifecycleScope.launch(Dispatchers.IO) {
            note = noteDatabase.noteDao().getNoteById(noteId) ?: return@launch

            withContext(Dispatchers.Main) {
                titleEditText.setText(note.title)
                contentEditText.setText(note.content)

                if (note.imageUri?.isNotEmpty() == true) {
                    Glide.with(this@DetailActivity)
                        .load(Uri.parse(note.imageUri))
                        .centerCrop()
                        .into(imageView)
                } else {
                    imageView.visibility = View.GONE
                }
            }
        }

        // 保存按钮点击事件
        findViewById<MaterialButton>(R.id.save_note_button).setOnClickListener {
            lifecycleScope.launch(Dispatchers.IO) {
                val updatedTitle = titleEditText.text.toString().trim()
                val updatedContent = contentEditText.text.toString().trim()

                if (updatedTitle.isNotEmpty()) {
                    note.title = updatedTitle
                    note.content = updatedContent

                    noteDatabase.noteDao().updateNote(note)

                    withContext(Dispatchers.Main) {
                        Toast.makeText(this@DetailActivity, "笔记已保存", Toast.LENGTH_SHORT).show()
                        finish()
                    }
                } else {
                    withContext(Dispatchers.Main) {
                        Toast.makeText(this@DetailActivity, "标题不能为空", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }

        // 取消/返回按钮点击事件
        findViewById<MaterialButton>(R.id.delete_note_button).setOnClickListener {
            finish() // 直接返回 MainActivity
        }
    }
}
